package com.ht.sps.nh.module;

import com.ht.sps.nh.bui.model.SimulateSideEnumNode;
import com.ht.sps.nh.entity.TreeMenu;
import com.ht.sps.nh.service.TreeMenuService;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 区域设置模块
 *
 * @author: 范博 13-11-15 下午2:30
 * To change this template use File | Settings | File Templates.
 */
@IocBean
@At("/nh/system/treeMenu")
public class TreeMenuModule {
    private static final Logger log = LoggerFactory.getLogger(TreeMenuModule.class);

    @Inject
    private TreeMenuService treeMenuService;

    /**
     * 主页面入口
     * @param request
     */
    @At("/")
    @Ok("th:nh/system/treeMenu") // 成功时进入的thymeleaf视图
    public void index(HttpServletRequest request) {
        log.debug("============Enter TreeMenuModule.index============");

    }

    public void setTreeMenuService(TreeMenuService treeMenuService) {
        this.treeMenuService = treeMenuService;
    }


    @At
    @Ok("fastjson")
    public List<SimulateSideEnumNode> getSideEnumNode(){

        List<SimulateSideEnumNode> simlateSideEnumNodes = new ArrayList();
        //数据接口获得数据为已排序好的数据及pid,level,id

//        //数据封装按三级数据封装
//        List<SimulateSideEnumNode> simlateSideEnumNodes3 = new ArrayList<>();
//        simlateSideEnumNodes3.add(new SimulateSideEnumNode(5,7,"三级功能1",3,null,null));
//        simlateSideEnumNodes3.add(new SimulateSideEnumNode(5,8,"三级功能2",3,null,null));
//        simlateSideEnumNodes3.add(new SimulateSideEnumNode(5,9,"三级功能3",3,null,null));
//
//        List<SimulateSideEnumNode> simlateSideEnumNodes2 = new ArrayList<>();
//
//        simlateSideEnumNodes2.add(new SimulateSideEnumNode(2,4,"二级功能1",2,null,null));
//        simlateSideEnumNodes2.add(new SimulateSideEnumNode(2,5,"二级功能2",2,null,simlateSideEnumNodes3));
//        simlateSideEnumNodes2.add(new SimulateSideEnumNode(2,6,"二级功能3",2,null,null));
//
//        simlateSideEnumNodes.add(new SimulateSideEnumNode(0,1,"首页",1,null,null));
//        simlateSideEnumNodes.add(new SimulateSideEnumNode(0,2,"一级功能1",1,null,simlateSideEnumNodes2));
//        simlateSideEnumNodes.add(new SimulateSideEnumNode(0,3,"一级功能2",1,null,null));


        List<TreeMenu> treeMenus = treeMenuService.getTreeMenuList();
        for (TreeMenu tm:treeMenus){
           if (tm.getMenuLevel() == 1) {
               simlateSideEnumNodes.add(new SimulateSideEnumNode(tm));
           } else if (tm.getMenuLevel() == 2) {
               for (SimulateSideEnumNode sse : simlateSideEnumNodes) {
                   if (sse.getId() == tm.getParentId()){
                       sse.getItems().add(new SimulateSideEnumNode(tm));
                       break;
                   }
               }
           } else if (tm.getMenuLevel() == 3) {
               boolean isHave = false;
               for (SimulateSideEnumNode sse : simlateSideEnumNodes) {

                   for (SimulateSideEnumNode secondSse :sse.getItems()){
                       if (secondSse.getId() == tm.getParentId()){
                           secondSse.getItems().add(new SimulateSideEnumNode(tm));
                           isHave = true;
                           break;
                       }
                   }
                   if (isHave){
                       break;
                   }
               }
           }
        }


        return simlateSideEnumNodes;
    }

}

